<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;
use think\Db;

class SysMenuModel extends Model
{
    
    protected $name = 'sys_menu';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time



   
    public function edit($param)
    {
        try{

            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysmenu/index'), '修改菜单成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param)
    {
        try{
          
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysmenu/index'), '添加菜单成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id)
    {
        return $this->where('id', $id)->find();
    }
    
    public function delById($id)
    {
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

  










    public function getActionWhere($where)
    {
        return $this->where($where)->find();
    }

   

    public function getListbyRoleId($roleid)
    {
        $para['isdelete']=0;
        if($roleid>0){
        	 $para['showstatus']=1;
        }
       
        

        $role=Db::name('sys_role')->where('id',$roleid)->find();

        if($roleid==0||$roleid==1)
        {
            $result =  $this->where($para)->order('ordernum asc')->select();
            return $result;

        }


        if(!empty($role))
        {
           
          

            $menut=  config('database.prefix')."sys_role";

            $idstr=" FIND_IN_SET(id , (select menulist from ".$menut." where id=".$roleid."))";
            $result =  $this->where($para)->where($idstr)->order('ordernum asc')->select();

            $t=$this->getLastSql();
            return $result;

        }

   
        return null;

    }

  


}
